﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NFixedPoint
{
    /// <summary>
    /// 弧度的定点数存法
    /// </summary>
    public struct Factor
    {
        int raw;
        uint multiplier;


        public Factor(float degree)
        {
            multiplier = 10000;
            raw = (int)(degree * multiplier);
        }

        #region 常量

        public static Factor Zero = new Factor(0);
        public static Factor One = new Factor(1);
        public static Factor HalfPi = new Factor(1.5708f);
        public static Factor Pi = new Factor(3.1416f);
        public static Factor TwoPi = new Factor(6.2832f);

        #endregion

        #region 运算符重载

        public static bool operator >(Factor a, Factor b)
        {
            return a.raw > b.raw;
        }
        public static bool operator <(Factor a, Factor b)
        {
            return a.raw < b.raw;
        }
        public static bool operator >=(Factor a, Factor b)
        {
            return a.raw >= b.raw;
        }
        public static bool operator <=(Factor a, Factor b)
        {
            return a.raw <= b.raw;
        }
        public static bool operator ==(Factor a, Factor b)
        {
            return a.raw == b.raw;
        }
        public static bool operator !=(Factor a, Factor b)
        {
            return a.raw != b.raw;
        }
        #endregion

        #region 其他
        public float ToDegree()
        {
            float radian = raw * 1.0f / multiplier;
            return (int)Math.Round(radian / Math.PI * 180);
        }
        public float AsFloat()
        {
            return raw * 1.0f / multiplier;
        }
        public override bool Equals(object obj)
        {
            return this == (Factor)obj;
        }

        public override int GetHashCode()
        {
            return raw.GetHashCode();
        }

        public override string ToString()
        {
            return ToDegree().ToString();
        }

        #endregion
    }
}
